<template>
    <el-dialog title="开易拍扫描/人像" :visible.sync="kt600Visible" width="80%" :modal="isShowModal">
        <div>
            <!--<div>
                <img :src="kt600PriPreviewPhoto" class="kt600Img">
                <img :src="kt600SubPreviewPhoto" class="kt600Img">
            </div>
            <div>
                &lt;!&ndash;<el-checkbox @change="setdeskew">纠偏裁边</el-checkbox>
                <el-checkbox @change="discernOcr">OCR</el-checkbox>&ndash;&gt;
                <el-button type="primary" @click="changePhoto" class="kt600Button">转换摄像头</el-button>
            </div>-->
            <div>
                <!--<el-button type="primary" @click="rotateLeft">左转</el-button>
                <el-button type="primary" @click="rotateRight">右转</el-button>-->
                <el-button type="primary" @click="scan" v-show="showScan">证件扫描</el-button>
                <el-button type="primary" @click="photo" v-show="showPhoto">人像采集</el-button>
            </div>
            <div>
                <img :src="kt600PriPhoto" class="kt600Img">
                <img :src="kt600SubPhoto" class="kt600Img">
            </div>
            <div>
                <el-input type="textarea" v-model="ocrResult" class="kt600Textarea" rows="5" :disabled="true"></el-input>
            </div>
            <el-button type="primary" @click="photoConfirm">确定</el-button>
            <el-button type="primary" @click="photoCancel">取消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    export default {
        name: "kt600PhotoComp",
        data(){
            return {
                kt600Visible:false,
                kt600Dialog:{},
                kt600PriPreviewPhoto:"",
                kt600SubPreviewPhoto:"",
                kt600PriPhoto:"",
                kt600SubPhoto:"",
                ocrResult:"",
                currentPhoto:1,
                showScan:true,
                showPhoto:true,
                isShowModal:true,
                imgList:[]
            }
        },
        methods:{
            /*rotateLeft:function () {
                let param = {"code": "1109"};
                this.$emit("kt600handle", param);
            },
            rotateRight:function () {
                let param = {"code": "1110"};
                this.$emit("kt600handle", param);
            },*/
            scan:function () {
                let param = {"code": "1001"};
                param.currentPhoto = 1;
                this.$emit("kt600handle", param);
            },
            photo:function () {
                let param = {"code": "1001"};
                param.currentPhoto = 2;
                this.$emit("kt600handle", param);
            },
            /*setdeskew:function () {
                let param = {"code": "1112"};
                this.$emit("kt600handle", param);
            },
            discernOcr:function () {
                let param = {"code": "1121"};
                this.$emit("kt600handle", param);
            },
            changePhoto : function(){
                if(this.currentPhoto == 1){
                    let param = {"code": "1105"};
                    this.$emit("kt600handle", param);
                    param = {"code": "1133"};
                    this.$emit("kt600handle", param);
                    this.currentPhoto = 2;
                    this.kt600PriPreviewPhoto = "";
                    this.showScan = false;
                    this.showPhoto = true;
                }else{
                    let param = {"code": "1105"};
                    this.$emit("kt600handle", param);
                    param = {"code": "1132"};
                    this.$emit("kt600handle", param);
                    this.currentPhoto = 1;
                    this.kt600SubPreviewPhoto = "";
                    this.showScan = true;
                    this.showPhoto = false;
                }
            },*/
            photoConfirm:function () {
                if(this.kt600PriPhoto){
                    let scanReturnData = {"retcode":"0","retinfo":""};
                    let scanData = {};
                    let scanImgArray = [];
                    scanImgArray.push(this.kt600PriPhoto);
                    for(let i=0;i<scanImgArray.length;i++){
                        scanData.cardImg = this.kt600PriPhoto;
                        scanData.businessJson = "{\"type\":\"0\",\"name\":\"证件扫描\",\"index\":\"0\"}";
                        scanData.ocrResult = this.parseOcrResult(this.ocrResult);
                        scanData.Content = scanImgArray[i];
                        scanReturnData.data = JSON.stringify(scanData);
                        this.$emit("kt600PhotoComplete", scanReturnData);
                    }
                }
                if(this.kt600SubPhoto){
                    let photoReturnData = {"retcode":"0","retinfo":""};
                    let photoData = {};
                    let photoImgArray = [];
                    photoImgArray.push(this.kt600SubPhoto);
                    for(let j=0;j<photoImgArray.length;j++){
                        photoData.faceImg = this.kt600SubPhoto;
                        photoData.businessJson = "{\"type\":\"1\",\"name\":\"人像采集\",\"index\":\"1\"}";
                        photoData.ocrResult = this.parseOcrResult(this.ocrResult);
                        photoData.Content = photoImgArray[j];
                        photoReturnData.data = JSON.stringify(photoData);
                        this.$emit("kt600PhotoComplete", photoReturnData);
                    }
                }
                this.kt600Visible = false;
                let param = {"code": "1002"};
                this.$emit("kt600handle", param);
            },
            photoCancel:function () {
                this.kt600Visible = false;
            },
            initkt600Photo : function () {
                /*let this_ = this;
                setInterval(function () {
                    let param = {"code": "1134"};
                    this_.$emit("kt600handle", param);
                }, 1000);*/
            },
            parseOcrResult: function (data) {
                //let keyList = this.keyList;
                console.info(data);
                let ocrResult = {};
                /*for(let i=0;i<keyList.length;i++){
                    let startIndex = data.indexOf(keyList[i].startKey);
                    if(startIndex != -1) {
                        startIndex = startIndex + keyList[i].startKey.length;
                    }
                    let endIndex = null;
                    if(keyList[i].endKey != null && keyList[i].endKey != ""){
                        endIndex = data.indexOf(keyList[i].endKey);
                    }
                    if(startIndex != -1 && endIndex != -1){
                        let curOcrResult;
                        if(endIndex == null){
                            curOcrResult = data.substring(startIndex);
                        }else{
                            curOcrResult = data.substring(startIndex, endIndex);
                        }
                        if(curOcrResult != null && curOcrResult != ""){
                            ocrResult[keyList[i].name] = curOcrResult;
                        }
                    }
                }*/
                return ocrResult;
            }
        },
        watch: {
            kt600Visible(val) {
                if(val == false){
                    let param = {"code": "1002"};
                    this.$emit("kt600handle", param);
                }
            }
        }
    }
</script>

<style scoped>
    .kt600Img{
        width: 49%;
        height: 480px;
        border: black 1px solid;
        margin: 2px 2px 2px 0px;
    }
    .kt600Textarea{
        width: 98.3%;
        height: 117px;
        border: black 1px solid;
        margin: 0px 0px 2px 0px;
    }
    .kt600Button{
        margin: 0px 0px 2px 0px;
    }
</style>